<?php
require_once(dirname(__FILE__)."/../config/include.php");
	
$GLOBALS['DEBUG_SQL_LOG'] = false ;

$time = date("Ymd H:i:s") ;
$save_log_filename = dirname(__FILE__)."/log_core_user_invite_totle.txt" ;
file_put_contents($save_log_filename,"$time -- 开始重新生成--用户邀请信息汇总表中数据 .\r\n",FILE_APPEND);

sql_connect() ;

$start = 0 ;
$limit = 50 ;
// 先处理所有用户的一级用户
while(true){
	$userlist = sql_fetch_rows("select `id` from `user_info` order by `id` asc limit $start , $limit") ;
	$no = 0 ;
	if (is_array($userlist)) foreach($userlist as &$u){
		$no ++ ;
		// 处理用户
		$uid = $u['id'] ;
		$uinvite = sql_fetch_one_cell("select `invite` from user_invite where `uid` = '$uid'") ;
		if (strlen($uinvite) == 0)		
		{
			file_put_contents($save_log_filename,"$time -- 用户  $uid 没有邀请码. .\r\n",FILE_APPEND);		
			continue ;
		}

        $rnt_frd = get_friend_count($uinvite) ;
        $one_friends = $rnt_frd['friends'];
        $one_payamounts = $rnt_frd['payamounts'] ;

        $my_payamounts = intval(sql_fetch_one_cell("select sum(`price_modify`) from `pay_trade` where `user_id` = '$uid' and `price_state` = 1")) ;

		sql_query("INSERT INTO `user_invite_totle` (`uid`, `my_payamounts` , `one_friends`, `one_payamounts`) VALUES ('$uid', '$my_payamounts' , '$one_friends', '$one_payamounts') on DUPLICATE KEY UPDATE `my_payamounts` = '$my_payamounts' , `one_friends` = '$one_friends' , `one_payamounts` = '$one_payamounts'") ;
		file_put_contents($save_log_filename,"$time -- No:$no [$uid]的一级用户数据处理完成 .人数:$one_friends , 消费和:$one_payamounts \r\n",FILE_APPEND);
	}else{
		break ;
	}
	if (count($userlist) != $limit){
		break ;
	}
	$start += $limit ;
}



$start = 0 ;
$limit = 50 ;
// 处理所有二级用户信息
while(true){
	$userlist = sql_fetch_rows("select `id` from `user_info` order by `id` asc limit $start , $limit") ;
	$no = 0 ;
	if (is_array($userlist)) foreach($userlist as &$u){
		$no ++ ;
		// 处理用户
		$uid = $u['id'] ;
		$uinvite = sql_fetch_one_cell("select `invite` from user_invite where `uid` = '$uid'") ;
		if (strlen($uinvite) == 0)		
		{
			file_put_contents($save_log_filename,"$time -- 用户  $uid 没有邀请码. .\r\n",FILE_APPEND);		
			continue ;
		}

        $rnt_frd = get_friend_count($uinvite , false) ;
        $two_friends = $rnt_frd['friends'];
        $two_payamounts = $rnt_frd['payamounts'] ;

		sql_query("INSERT INTO `user_invite_totle` (`uid`, `two_friends`, `two_payamounts`) VALUES ('$uid', '$two_friends', '$two_payamounts') on DUPLICATE KEY UPDATE `two_friends` = '$two_friends' , `two_payamounts` = '$two_payamounts'") ;
		file_put_contents($save_log_filename,"$time -- NoT:$no [$uid]的二级用户数据处理完成 .人数:$two_friends , 消费和:$two_payamounts \r\n",FILE_APPEND);
	}else{
		break ;
	}
	if (count($userlist) != $limit){
		break ;
	}
	$start += $limit ;
}


function get_friend_count($invite , $isOne = true)
{
    $start = 0 ;
    $limit = 50 ;

    $friends = 0;
    $payamounts = 0 ;
	while(true){
        // 使用我的邀请码的用户 (一级用户)
        $one_users_info = sql_fetch_rows("select u.uid from user_invite u inner join user_info i on u.uid = i.id where u.invite_f = '$invite' order by u.uid limit $start , $limit") ;
        // 一级用户的id列表
        $userids = "" ; $dh = "" ;
        if (is_array($one_users_info)) foreach($one_users_info as &$uf){
            $userids .= $dh.$uf['uid'] ;  $dh = "," ;
        }
        if (strlen($userids) > 0){

        	if ($isOne){
                $payamounts += intval(sql_fetch_one_cell("select sum(`price_modify`) from `pay_trade` where `user_id` in ($userids) and `price_state` = 1")) ;
                $friends += count($one_users_info);
			}else {
                // 人数及消费总额
                $info = sql_fetch_one("select sum(`one_friends`) as tows , sum(`one_payamounts`) as towp from `user_invite_totle` where `uid` in ($userids)");
                if (is_array($info)) {
                    $friends += intval($info['tows']);
                    $payamounts += intval($info['towp']);
                }
            }
        }

        if (count($one_users_info) != $limit){
            break ;
        }
        $start += $limit ;
	}

	$rnt = array() ;
	$rnt['friends'] 	= $friends ;
    $rnt['payamounts'] 	= $payamounts ;
	return $rnt ;
}

?>
